package com.linux.huhx.server;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;

/**
 * @Author: huhx
 * @Date: 2017-11-06 下午 5:11
 */
public class ZkNodeExists {
    private static ZooKeeper zooKeeper;
    private static ZooKeeperConnection zooKeeperConnection;

    public static Stat ZkNodeExists(String path) throws KeeperException, InterruptedException {
        return zooKeeper.exists(path, true);
    }

    public static void main(String[] args) {
        String path = "/HuhxZkNode";

        try {
            zooKeeperConnection = new ZooKeeperConnection();
            zooKeeper = zooKeeperConnection.connect("localhost");
            Stat stat = ZkNodeExists(path);
            if (stat != null) {
                System.out.println("Node exists and the node version is " + stat.getVersion());
            } else {
                System.out.println("Node doesn't exists.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
